<?php
            
use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 


/*
 *参与用户
 * */
class Fans_Page extends PluginWebPage {
    function main()
    {
        $method = 'gift_fans';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $wlist = Worker::getworkerlist($system,$themeid);
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;

        $type = $_P['type']?intval($_P['type']):0;
        $classify = $_P['classify']?$_P['classify']:'';
        $gift_id = $_P['gift_id']?intval($_P['gift_id']):0;
        $status = $_P['status']?intval($_P['status']):0;

        $gift_config = GiftModel::getGiftConfig($themeid);

        $where_arr['type'] = $type;
        $where_arr['classify'] =$classify;
        $where_arr['gift_id'] =$gift_id;
        $where_arr['status'] =$status;
        extract(giftModel::getGiftCount($themeid,$where_arr));

        $has_tuan_gift = 0;
        if(count($tuan_gift)>1){
            $has_tuan_gift = 1;
        }
        $has_invite_gift = 0;
        if(count($invite_gift)>1){
            $has_invite_gift  = 1;
        }
        $has_help_gift = 0;
        if(count($help_gift)>1){
            $has_help_gift  = 1;
        }

        $total = giftModel::getGainUserList($system,$config,$themeid,$where_arr,0,0,true,false);
        if(checksubmit('export_submit', true)){
            $count = $total['count'];
            $header = array(
                'uid' => 'UID',
                'nickname' => '昵称',
                'username' => '姓名',
                'mobile' => '电话',
            );

            if($has_tuan_gift){
                $header['tuan_gift'] = '成团礼';
                $header['tuan_exchange_gift'] = '已核销成团礼';
            }
            if($has_invite_gift){
                $header['invite_gift'] = '邀请下单';
                $header['invite_exchange_gift'] = '已核销邀请下单礼品';
            }
            if($has_invite_gift){
                $header['help_gift'] = '邀请助力';
                $header['help_exchange_gift'] = '已核销邀请助力礼品';
            }
            $header['pay_status'] = '付款状态';
            if ($config['yukuan'] > 0 && TIMESTAMP > $config['yukuantime']) {
                $header['bukuan'] = '补款状态';
            }
            $header['worker'] = '员工';
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";

            $ex_page_index = max(1,intval($_P['ex_page']));
            $ex_page_size = 5000;
            $ex_page_start = ($ex_page_index-1)*$page_size;
            $page_total = ceil($count/$ex_page_size);
            $list = giftModel::getGainUserList($system,$config,$themeid,$where_arr,$ex_page_start,$ex_page_size,false,true);
            foreach ($list as $k => $row) {
                $row['nickname'] = str_replace(',','',$row['nickname']);
                $row['nickname'] = str_replace('"','',$row['nickname']);
                foreach ($keys as $key) {
                    $data[] = $row[$key];
                }
                if (isset($data)){
                    $user[] = implode("\t ,", $data) . "\t ,";
                    unset($data);
                }
                $list[$k]['bukuan'] = $row['pay_status']['bukuan'];
                $list[$k]['pay_status'] = $row['pay_status']['txt'];
            }
            $html .= implode("\n", $user)."\n";
            //新版本导出合并
            $is_end = 0;
            if($ex_page_index>=$page_total){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'邀请礼参与用户记录',$themeid,$is_end);
        }
        $page_total = $total['count'];
        $list = giftModel::getGainUserList($system,$config,$themeid,$where_arr,$page_start,$page_size,false,false);
        $return_clicent = get_defined_vars();
return $this->view('/gift/fans/main', $return_clicent);
    }

}